﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace hw1.hw03
{
    class Program
    {
        static void Main(string[] args)
        {
            AlarmClock alarmClock = new AlarmClock();

            // 订阅Alarm事件
            alarmClock.Alarm += new EventHandler<AlarmEventArgs>(Alarm_handler);

            // 设置闹钟
            DateTime alarmTime;
            while (true)
            {
                Console.WriteLine("请设定闹钟：\n日期：（年-月-日）");
                String alarm;
                alarm = Console.ReadLine();
                alarm += " ";
                Console.WriteLine("时间：（时:分:秒）");
                alarm += Console.ReadLine();
                try
                {
                    alarmTime = Convert.ToDateTime(alarm);
                    break;
                }
                catch (FormatException e) { Console.WriteLine("输入日期或时间不合法"); }
            }
            alarmClock.SetAlarmTime(alarmTime);
            Console.WriteLine($"闹钟已设定，将在"+ alarmTime.ToString()+"响铃..."); 

            // 订阅Tick事件
            alarmClock.Tick += new EventHandler<TickEventArgs>(Tick_handler);

            Console.ReadLine(); // 防止程序退出
        }

        static void Tick_handler(object sender, TickEventArgs e)
        {
            Console.WriteLine($"Tick: {e.CurrentTime:HH:mm:ss}");
        }

        static void Alarm_handler(object sender, EventArgs e)
        {
            Console.WriteLine("闹钟时间已到！");
        }
    }
}
